home *** CD-ROM | disk | FTP | other *** search
/ Developer CD Series 2000 November: Tool Chest / Dev.CD Nov 00 TC Disk 1.toast / Sample Code / Graphics 2D / Direct Pixel Access / Direct Pixel Access.c < prev    next >
Encoding:
C/C++ Source or Header  |  2000-09-28  |  7.6 KB  |  289 lines  |  [TEXT/CWIE]

  1. /*
  2.     File:        Direct Pixel Access.c
  3.  
  4.     Contains:    This snippet shows one example of how to directly            
  5.                 change the pixel values stored in a pixel image.            
  6.                 The original pixel image is obtained from a 'icl8'            
  7.                 resource.  Only the first 20 columns of the first        
  8.                 20 rows of the 'icl8' image is used.    
  9.  
  10.     Written by: EL    
  11.  
  12.     Copyright:    Copyright © 1992-1999 by Apple Computer, Inc., All Rights Reserved.
  13.  
  14.                 You may incorporate this Apple sample source code into your program(s) without
  15.                 restriction. This Apple sample source code has been provided "AS IS" and the
  16.                 responsibility for its operation is yours. You are not permitted to redistribute
  17.                 this Apple sample source code as "Apple sample source code" after having made
  18.                 changes. If you're going to re-distribute the source, we require that you make
  19.                 it clear in the source that the code was descended from Apple sample source
  20.                 code, but that you've made changes.
  21.  
  22.     Change History (most recent first):
  23.                 08/2000        JM                Carbonized, non-Carbon code is commented out
  24.                                             for demonstration purposes.
  25.                 7/9/1999    KG                Updated for Metrowerks Codewarror Pro 2.1
  26.                 
  27.  
  28. */
  29. #include "CarbonPrefix.h"
  30. #include <Dialogs.h>
  31. #include <Fonts.h>
  32. #include <Resources.h>
  33. #include <QDOffscreen.h>
  34. #include <TextUtils.h>
  35. #include <Quickdraw.h>
  36. /* Constant Declarations */
  37.  
  38. #define    WWIDTH    600
  39. #define    WHEIGHT    400
  40.  
  41. //#define WLEFT    (((qd.screenBits.bounds.right - qd.screenBits.bounds.left) - WWIDTH) / 2)
  42. //#define WTOP    (((qd.screenBits.bounds.bottom - qd.screenBits.bounds.top) - WHEIGHT) / 2)
  43.  
  44. /* Global Variable Definitions */
  45.  
  46. WindowPtr        gWindow;
  47. PixMapHandle    gPixmap;
  48.  
  49. void initMac();
  50.  
  51. void createWindow();
  52. void createOffscreen();
  53. void drawPixelImageData();
  54. void doEventLoop();
  55.  
  56. void main(void)
  57. {
  58.     initMac();
  59.     
  60.     createWindow();
  61.     createOffscreen();
  62.     
  63.     doEventLoop();
  64. }
  65.  
  66. void initMac()
  67. {
  68.     //MaxApplZone();
  69.  
  70.     /*InitGraf( &qd.thePort );
  71.     InitFonts();
  72.     InitWindows();
  73.     InitMenus();
  74.     TEInit();
  75.     InitDialogs( nil );*/
  76.     InitCursor();
  77.     FlushEvents( 0, everyEvent );
  78. }
  79.  
  80. void createWindow()
  81. {
  82.     Rect        rect;
  83.     BitMap        bitMap;
  84.     Rect        tempRect1;
  85.     int            top, left;
  86.     
  87.     GetQDGlobalsScreenBits(&bitMap);
  88.     tempRect1 = bitMap.bounds;
  89.     
  90.     left = (((tempRect1.right - tempRect1.left) - WWIDTH) / 2);
  91.     top = (((tempRect1.bottom - tempRect1.top) - WHEIGHT) / 2);
  92.  
  93.     //SetRect( &rect, WLEFT, WTOP, WLEFT + WWIDTH, WTOP + WHEIGHT );
  94.     SetRect( &rect, left, top, left + WWIDTH, top + WHEIGHT );
  95.     gWindow = NewCWindow( 0L, &rect, "\pDirect Pixel Access", true, documentProc,
  96.                             (WindowPtr)-1L, true, 0L );                        
  97.     //SetPort( gWindow );
  98.     SetPortWindowPort( gWindow );
  99.     
  100.     TextFont( kFontIDGeneva );
  101.     TextSize( 9 );
  102.     TextMode( srcXor );
  103. }
  104.  
  105. void createOffscreen()
  106. {
  107.     Rect        rect;
  108.     Handle        iclHandle;
  109.     char        *image;
  110.     int            row, col, index, value;
  111.     
  112.     
  113.     SetRect( &rect, 0, 0, 32, 32 );
  114.     
  115.     /* Create offscreen pixmap image using an 'icl8' icon resource. */
  116.  
  117.     iclHandle = GetResource( 'icl8', 129 );
  118.     HLock( iclHandle );
  119.     HNoPurge( iclHandle );
  120.     
  121.     //gPixmap = (PixMapHandle)NewHandle( sizeof( PixMap ) );
  122.     gPixmap = NewPixMap();
  123.     
  124.     (**gPixmap).baseAddr = *iclHandle;
  125.     (**gPixmap).rowBytes = ((32 * 8) / 8) | 0x8000;
  126.     (**gPixmap).bounds = rect;
  127.     (**gPixmap).pmVersion = 0;
  128.     (**gPixmap).packType = 0;
  129.     (**gPixmap).packSize = 0;
  130.     (**gPixmap).hRes = 72;
  131.     (**gPixmap).vRes = 72;
  132.     (**gPixmap).pixelSize = 8;
  133.     //(**gPixmap).planeBytes = 0;
  134.     //(**gPixmap).pmReserved = 0;
  135.     (**gPixmap).pixelType = 0;
  136.     (**gPixmap).cmpCount = 1;
  137.     (**gPixmap).cmpSize = 8;
  138.     (**gPixmap).pmTable = GetCTable( 8 );
  139.     (**gPixmap).pixelFormat = 0;
  140.     
  141.     /* Give a unique seed for the pixmap's colortable. */
  142.     (**(**gPixmap).pmTable).ctSeed = GetCTSeed();
  143.     
  144.     SetRect( &rect, 0, 0, 20, 20 );
  145.     
  146.     /* Set the pointer to the beginning of the pixel image. */
  147.     image = GetPixBaseAddr( gPixmap );
  148.     
  149.     /*****************************************************************/
  150.     /* For this example, let's set the pixel values of the left half */
  151.     /*   of the image to half their original values.                 */
  152.     /*****************************************************************/
  153.     
  154.     for (row = 0; row < rect.bottom; row++)
  155.     {
  156.         // Loop through the first 10 columns of the pixel image. 
  157.         for (index = 0, col = 0; col < rect.right / 2; col++)
  158.         {
  159.             // Set the value at this index to half its value. 
  160.             value = (unsigned char)*(image + index);
  161.             *(image + index) = value / 2;
  162.             
  163.             index++;
  164.         }
  165.         
  166.         // Increment the pointer to the next row of the pixel image. 
  167.         image += ((**gPixmap).rowBytes & 0x7fff);
  168.     }
  169.  
  170. }
  171.  
  172. void drawPixelImageData()
  173. {
  174.     int                row, col;
  175.     Rect            rect;
  176.     unsigned char    value;
  177.     char            *image;
  178.     int                index = 0;
  179.     Str255            string;
  180.     RGBColor        color = { 32000, 32000, 32000 };
  181.     //Byte            mode;
  182.     Rect            tempRect1;
  183.     
  184.     ForeColor( blackColor );
  185.     
  186.     SetRect( &rect, 0, 0, 20, 20 );
  187.     
  188.     /* For this example, let's just use only the upper left corner of the image. */
  189.     
  190.     // Draw the offscreen image to the screen to see what it looks like.
  191.     //CopyBits( (BitMap *)*gPixmap, &gWindow->portBits, &rect,
  192.     //        &gWindow->portRect, srcCopy, 0 );
  193.     //(**gPixmap).rowBytes ^= 0x8000;
  194.     CopyBits( (BitMap *)*gPixmap, GetPortBitMapForCopyBits(GetWindowPort(gWindow)), &rect,
  195.             GetPortBounds(GetWindowPort(gWindow), &tempRect1), srcCopy, 0 );
  196.     //(**gPixmap).rowBytes ^= 0x8000;
  197.     
  198.     RGBForeColor( &color );
  199.     
  200.     // Again, set the pointer to the beginning of the pixel image.
  201.     image = GetPixBaseAddr( gPixmap );
  202.     
  203.     /***************************************************************/
  204.     /* Finally let's display the pixel values on top of the image. */
  205.     /***************************************************************/
  206.     
  207.     /* Loop through the first 20 rows of the pixel image. */
  208.     for (row = 0; row < rect.bottom; row++)
  209.     {
  210.         // Loop through the first 20 columns of the pixel image. 
  211.         for (index = 0, col = 0; col < rect.right; col++)
  212.         {
  213.             // Get the value at this index into the pixel image. 
  214.             value = (unsigned char)*(image + index);
  215.             
  216.             MoveTo( col * 30, row * 20 );
  217.             LineTo( col * 30, (row + 1) * 20 );
  218.             LineTo( (col + 1) * 30, (row + 1) * 20 );
  219.             
  220.             MoveTo( (col * 30) + 6, (row * 20) + 14 );
  221.             NumToString( (long)value, string );
  222.             DrawString( string );
  223.             
  224.             index++;
  225.         }
  226.         
  227.         // Increment the pointer to the next row of the pixel image. 
  228.         image += ((**gPixmap).rowBytes & 0x7fff);
  229.     }
  230. }
  231.  
  232. void doEventLoop()
  233. {
  234.     EventRecord event;
  235.     WindowPtr   window;
  236.     short       clickArea;
  237.     Rect        screenRect;
  238.     RgnHandle    rgnHandle = NewRgn();
  239.  
  240.     for (;;)
  241.     {
  242.         if (WaitNextEvent( everyEvent, &event, 0, nil ))
  243.         {
  244.             if (event.what == mouseDown)
  245.             {
  246.                 clickArea = FindWindow( event.where, &window );
  247.                 
  248.                 if (clickArea == inDrag)
  249.                 {
  250.                     //screenRect = (**GetGrayRgn ()).rgnBBox;
  251.                     GetRegionBounds(GetGrayRgn(), &screenRect);
  252.                     DragWindow( window, event.where, &screenRect );
  253.                 }
  254.                 else if (clickArea == inContent)
  255.                 {
  256.                     if (window != FrontWindow())
  257.                         SelectWindow( window );
  258.                 }
  259.                 else if (clickArea == inGoAway)
  260.                     if (TrackGoAway( window, event.where ))
  261.                         return;
  262.             }
  263.             else if (event.what == updateEvt)
  264.             {
  265.                 window = (WindowPtr)event.message;    
  266.                 //SetPort( window );
  267.                 SetPortWindowPort( window );
  268.                 
  269.                 BeginUpdate( window );
  270.                 drawPixelImageData();
  271.                 EndUpdate( window );
  272.                 QDFlushPortBuffer(GetWindowPort(window), GetPortVisibleRegion(GetWindowPort(window), rgnHandle));
  273.             }
  274.             else if (event.what == activateEvt) 
  275.             {
  276.                 /*if (event.modifiers & activeFlag) {
  277.                     window = (WindowPtr)event.message;
  278.                     SetPortWindowPort(window);
  279.                     drawPixelImageData();
  280.                     QDFlushPortBuffer(GetWindowPort(window), GetPortVisibleRegion(GetWindowPort(window), rgnHandle));
  281.                 }*/
  282.                 /*if (event.modifiers & activeFlag)
  283.                     PostEvent(updateEvt, (unsigned long)gWindow);*/
  284.             }
  285.         }
  286.     }
  287.     
  288.     DisposeRgn(rgnHandle);
  289. }